package com.sgb.merchant.viewmodel;

import android.app.Activity;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.entity.UserInfo;
import com.sgb.link.entity.merchant.DemandDetailEntity;
import com.sgb.link.entity.merchant.DemandListEntity;
import com.sgb.link.entity.merchant.InvestmentDetailEntity;
import com.sgb.link.router.PathMerchant;
import com.sgb.merchant.api.MerchantsNet;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DemandDetailViewModel extends BaseViewModel {
    public MutableLiveData<DemandDetailEntity> ldInfo=new MutableLiveData<>();
    public MutableLiveData<List<DemandListEntity>> liveList = new MutableLiveData<>();
    public MutableLiveData<UserInfo> ldUserInfo=new MutableLiveData<>();
    public MutableLiveData<BaseEntity> ldResult=new MutableLiveData<>();
    public MutableLiveData<Integer> ldState=new MutableLiveData<>();
    public MutableLiveData<Boolean> ldStatus=new MutableLiveData<>();
    public MutableLiveData<Boolean> ldDel=new MutableLiveData<>();
    public MutableLiveData<Integer> ldItemState=new MutableLiveData<>();
    public DemandDetailViewModel(@NonNull @NotNull Application application) {
        super(application);
    }
    public void getInfo(String no){
        MerchantsNet.getInstance().getService().getDemandsDetail(no).enqueue(new ICallBack<BaseEntity<DemandDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<DemandDetailEntity> data) {
                if (data.getData() == null ) {
                    return;
                }
                ldInfo.postValue(data.getData());
            }
        });
    }
    public void getRecommendsList(String no,String type){
        MerchantsNet.getInstance().getService().getDemandsRecommends(no,type).enqueue(new Callback<BaseEntity<ListResponse<List<DemandListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DemandListEntity>>>> call, Response<BaseEntity<ListResponse<List<DemandListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null || response.body().getData().getList()==null) {
                    return;
                }
                liveList.postValue(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DemandListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getImInfo(String no){
        MerchantsNet.getInstance().getService().getImInfo(no).enqueue(new Callback<BaseEntity<UserInfo>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserInfo>> call, Response<BaseEntity<UserInfo>> response) {
                DialogHelper.dismissProgressDialog();
                if(response.body()==null){
                    return;
                }
                if(response.body()!=null&&response.body().isSuccess()&&response.body().getData()!=null){
                    ldUserInfo.postValue(response.body().getData());
                }else{
                    MToast.showToast(getApplication(),response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<UserInfo>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void follow(long no){
        MerchantsNet.getInstance().getService().follow(no,"FT2").enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if(response.body()==null){
                    return;
                }
                if(response.body()!=null&&response.body().isSuccess()){
                    MToast.showToast(getApplication(),response.body().getMsg());
                    ldResult.postValue(response.body());
                }else{
                    MToast.showToast(getApplication(),response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getState(String no){
        MerchantsNet.getInstance().getService().getDemandsState(no).enqueue(new Callback<BaseEntity<Integer>>() {
            @Override
            public void onResponse(Call<BaseEntity<Integer>> call, Response<BaseEntity<Integer>> response) {
                DialogHelper.dismissProgressDialog();
                if(response.body().isSuccess()){
                    ldState.postValue(response.body().getData());
                }else{
                    MToast.showToast(getApplication(),response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Integer>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getItemState(String no){
        MerchantsNet.getInstance().getService().getDemandsState(no).enqueue(new Callback<BaseEntity<Integer>>() {
            @Override
            public void onResponse(Call<BaseEntity<Integer>> call, Response<BaseEntity<Integer>> response) {
                DialogHelper.dismissProgressDialog();
                if(response.body().isSuccess()){
                    ldItemState.postValue(response.body().getData());
                }else{
                    MToast.showToast(getApplication(),response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Integer>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void delProject(String no){
        MerchantsNet.getInstance().getService().delDemand(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if(data.isSuccess()){
                    MToast.showToast(getApplication(),"删除成功");
                    ldDel.postValue(true);
                }
            }
        });
    }
    public void updateDemands(String no,String status){
        MerchantsNet.getInstance().getService().updateDemands(no,status).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if(data.isSuccess()){
                    ldStatus.postValue(true);
                }
            }
        });
    }
    /**
     * 获取详情页并进行状态判断
     *
     * @param no
     */
    public void getDetailState(Activity context, String no, int type) {//0 编辑 1上架 2下架 3删除
        DialogHelper.showProgressDialog(context, "加载中").setCanceledOnTouchOutside(true);
        MerchantsNet.getInstance().getService().getDemandsState(no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if (data.isSuccess()) {
                    int status = data.getData();//0已下架 1已删除 2已上架
                    switch (type) {
                        case 0:
                            if (status != 0) {
                                ldStatus.postValue(false);
                            } else {
                                ActivityJump.startActivity(PathMerchant.PublishDemandActivity, Pair.create(RouterKey.NO,no));
                            }
                            break;
                        case 1:
                            if (status != 0) {
                                ldStatus.postValue(false);
                            } else {
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("确定上架该条信息？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 -> {
                                            updateDemands(no,"1");
                                        }).show();
                            }
                            break;
                        case 2:
                            if (status !=2) {
                                ldStatus.postValue(false);
                            } else {
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("下架该信息后，前台将不再继续展示，确定下架？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 -> {
                                            updateDemands(no,"0");
                                        }).show();
                            }
                            break;
                        case 3:
                            if (status != 0) {
                                ldStatus.postValue(false);
                            } else {
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("请问是否确认删除？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 -> {
                                            delProject(no);
                                        }).show();
                            }
                            break;
                    }


                }
            }
        });
    }


}
